package com.otoe.dilidilivid.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.otoe.dilidilivid.R;
import com.otoe.dilidilivid.data.dao.UserDao;
import com.otoe.dilidilivid.data.entity.User;

/**
 * function：连接页面加载首页
 */
public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "mysql-party-MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //管理员登录
        findViewById(R.id.btnAdmin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText EditTextAccount = findViewById(R.id.uesrAccount);
                EditText EditTextPassword = findViewById(R.id.userPassword);

                String uname=EditTextAccount.getText().toString();
                String password=EditTextPassword.getText().toString();
                //管理员登录
                if("admin".equals(uname)&&"123456".equals(password)){
                    startActivity(new Intent(LoginActivity.this, AdminActivity.class));
                }else{
                    Toast.makeText(LoginActivity.this,"管理员用户名密码错误", Toast.LENGTH_LONG);
                }
            }
        });
    }

    public void reg(View view){
        Intent intent;
        intent = new Intent(LoginActivity.this, RegisterActivity.class);
        startActivity(intent);
    }

    /**
     * function: 登录
     * */
    public void login(View view){
        EditText EditTextAccount = findViewById(R.id.uesrAccount);
        EditText EditTextPassword = findViewById(R.id.userPassword);

        String uname=EditTextAccount.getText().toString();
        String password=EditTextPassword.getText().toString();
        //普通用户登录
        new Thread(){
            @Override
            public void run() {
                UserDao userDao = new UserDao();
                //int msg = 0;
                Message msg = Message.obtain();
                User user=new User();

                try {
//                    String uname = EditTextAccount.getText().toString();
//                    String password=EditTextPassword.getText().toString();
                    user.setU_name(uname);
                    user.setPassword(password);
                    msg.obj = user;
                    msg.what = userDao.login(uname,password);
//                    View v= LayoutInflater.from(LoginActivity.this).inflate(R.layout.fragment_my,null);
//                    TextView tv = (TextView) v.findViewById(R.id.tv_name);
//                    tv.setText("  " +uname + "  ");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                hand1.sendMessage(msg);
            }
        }.start();

    }

    @SuppressLint("HandlerLeak")
    final Handler hand1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0){
                Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show();
            } else if (msg.what == 1) {
//                Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show();
                User user=(User)msg.obj;
                startMain(user);
            } else if (msg.what == 2){
                Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_LONG).show();
            } else if (msg.what == 3){
                Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_LONG).show();
            }
        }
    };

    public void startMain(User user){
        //跳转到主页面
        Intent intent = null;

        intent = new Intent(this, MainActivity.class);
        intent.putExtra("user",user);
        this.startActivity(intent);
    }
}